import sys
sys.path.append("../")
import datetime
from controller.ScoreCalcController import *
from frameworks.utils.RedisUtil import *
from services.DayKlineService import *
from frameworks.cores.LoadClass import *

class TradeController:
    def __init__(self):
        self.calc = ScoreCalcController()
        self.base_num = 10
        self.calc_num = 10
        self.redis = RedisUtil()
        self.kline = DayKlineService()
        self.load = LoadClass()

    def run(self):
        for i in range(self.base_num):
            n = self.base_num - i
            #初始化日期
            trade_date = datetime.datetime.now()-datetime.timedelta(days=n)
            endDay = trade_date.strftime("%Y-%m-%d")
            print(endDay)
            startDay = (trade_date - datetime.timedelta(days=self.base_num)).strftime("%Y-%m-%d")
            calcDay = (trade_date - datetime.timedelta(days=self.calc_num)).strftime("%Y-%m-%d")
            print(startDay)
            today = (trade_date + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
            #获取当前日期前10天内有涨停的股票分数详情
            print("===============" + startDay + "--" + endDay + "============")
            #"""
            #data = self.calc.calcOrder(startDay,endDay,calcDay)
            #"""
            #exit(0)
            ck = "score_calc_controller"
            data = json.loads(self.redis.vget(ck))
            allcodes = reversed(sorted(data, key=lambda x: x["score"]))
            codes = []
            for row in allcodes:
                code = row["code"][2:]
                marketCode = code[:2]
                if marketCode == "30" or marketCode == "68":
                    continue
                if int(row["score"]) > 100 and int(row["score"]) < 300:
                    codes.append(row)
            allrs = []
            print(codes)
            exit(0)
            for line in codes:
                curdata = self.kline.getDataByDate(line["code"],today,today)
                option = curdata[0]
                close = float(option["close"])
                open = float(option["open"])
                pre_close = float(option["pre_close"])
                zf = round(((open-pre_close)*100)/pre_close,2)
                if zf > 3 or zf < -3:
                    continue
                allrs.append(option)
                break
            print(allrs)
            break